﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QVBoxLayout>
#include <QRandomGenerator>

#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle("相位展示控件示例");

    setMinimumSize(30, 30);

    // 创建相位控件
    m_phaseWidget = new PhaseWidget(this);
    m_phaseWidget->setPhase(45.0);

    // 将相位控件添加到界面
    QVBoxLayout *layout = new QVBoxLayout(ui->phaseWidgetContainer);
    layout->addWidget(m_phaseWidget);
    layout->setContentsMargins(0, 0, 0, 0);

    // 初始化界面控件
    ui->phaseSlider->setValue(45);
    ui->phaseSpinBox->setValue(45.0);
    ui->editableCheckBox->setChecked(true);
    ui->animatedCheckBox->setChecked(true);
    ui->showLabelsCheckBox->setChecked(true);
    ui->showValueCheckBox->setChecked(true);

    // 连接信号和槽
    connect(ui->phaseSlider, &QSlider::valueChanged, this, &MainWindow::on_phaseSlider_valueChanged);
    connect(ui->phaseSpinBox, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &MainWindow::on_phaseSpinBox_valueChanged);
    connect(ui->setPhaseButton, &QPushButton::clicked, this, &MainWindow::on_setPhaseButton_clicked);
    connect(ui->editableCheckBox, &QCheckBox::toggled, this, &MainWindow::on_editableCheckBox_toggled);
    connect(ui->animatedCheckBox, &QCheckBox::toggled, this, &MainWindow::on_animatedCheckBox_toggled);
    connect(ui->showLabelsCheckBox, &QCheckBox::toggled, this, &MainWindow::on_showLabelsCheckBox_toggled);
    connect(ui->showValueCheckBox, &QCheckBox::toggled, this, &MainWindow::on_showValueCheckBox_toggled);
    connect(m_phaseWidget, &PhaseWidget::phaseChanged, this, &MainWindow::on_phaseWidget_phaseChanged);
    connect(ui->doubleSpinBox, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &MainWindow::on_doubleSpinBox_phaseChanged);

    connect(ui->overrangecheckBox, &QPushButton::toggled, this, &MainWindow::on_editableCheckBox_toggled);
    connect(ui->gridcheckBox, &QPushButton::toggled, this, &MainWindow::on_gridcheckBox_toggled);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_phaseSlider_valueChanged(int value)
{
    m_phaseWidget->setPhaseAnimated(value);
    ui->phaseSpinBox->setValue(value);
}

void MainWindow::on_phaseSpinBox_valueChanged(double arg1)
{
    m_phaseWidget->setPhaseAnimated(arg1);
    ui->phaseSlider->setValue(arg1);
}

void MainWindow::on_setPhaseButton_clicked()
{
    // double randomPhase = QRandomGenerator::global()->bounded(0.0, 360.0);
    // 将参数转换为int，生成整数随机数再转换回double
    double randomPhase = static_cast<double>(QRandomGenerator::global()->bounded(0, 360));
    m_phaseWidget->setPhaseAnimated(randomPhase);
    ui->phaseSlider->setValue(randomPhase);
    ui->phaseSpinBox->setValue(randomPhase);
}

void MainWindow::on_editableCheckBox_toggled(bool checked)
{
    m_phaseWidget->setEditable(checked);
}

void MainWindow::on_overrangecheckBox_toggled(bool checked)
{
    m_phaseWidget->setShowOverrange(checked);
}

void MainWindow::on_gridcheckBox_toggled(bool checked)
{
    m_phaseWidget->setShowGrid(checked);
}

void MainWindow::on_animatedCheckBox_toggled(bool checked)
{
    m_phaseWidget->setAnimated(checked);
}

void MainWindow::on_showLabelsCheckBox_toggled(bool checked)
{
    m_phaseWidget->setShowAngleLabels(checked);
}

void MainWindow::on_showValueCheckBox_toggled(bool checked)
{
    m_phaseWidget->setShowPhaseValue(checked);
}

void MainWindow::on_phaseWidget_phaseChanged(double phase)
{
    // 更新界面显示
    ui->phaseSlider->setValue(phase);
    ui->phaseSpinBox->setValue(phase);
}

void MainWindow::on_doubleSpinBox_phaseChanged(double d)
{
    m_phaseWidget->setReferenceValue(d);
}
